package com.jeesite.modules.exam.score.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.jeesite.common.idgen.IdGen;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuotaAdd;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.score.dao.DcSupExamSumScoreDao;
import com.jeesite.modules.exam.score.entity.DcSupExamSumScore;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.EmpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.exam.score.entity.DcSupExamOrgScore;
import com.jeesite.modules.exam.quota.dao.DcSupExamOrgScoreDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

import javax.validation.constraints.NotBlank;

/**
 * 考核任务评分-被考核单位总分Service
 *
 * @author xhc
 * @version 2022-05-28
 */
@Slf4j
@Service
@Transactional(readOnly=false)
@RequiredArgsConstructor
public class DcSupExamOrgScoreService extends CrudService<DcSupExamOrgScoreDao, DcSupExamOrgScore> {

    private final DcSupExamSumScoreDao dcSupExamSumScoreDao;
    private final DcSupExamOrgScoreDao dcSupExamOrgScoreDao;

    /**
     * 获取单条数据
     *
     * @param dcSupExamOrgScore
     * @return
     */
    @Override
    public DcSupExamOrgScore get(DcSupExamOrgScore dcSupExamOrgScore) {
        return super.get(dcSupExamOrgScore);
    }

    /**
     * 查询分页数据
     *
     * @param dcSupExamOrgScore      查询条件
     * @param dcSupExamOrgScore.page 分页对象
     * @return
     */
    @Override
    public Page<DcSupExamOrgScore> findPage(DcSupExamOrgScore dcSupExamOrgScore) {
        return super.findPage(dcSupExamOrgScore);
    }

    /**
     * 查询列表数据
     *
     * @param dcSupExamOrgScore
     * @return
     */
    @Override
    public List<DcSupExamOrgScore> findList(DcSupExamOrgScore dcSupExamOrgScore) {
        return super.findList(dcSupExamOrgScore);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param dcSupExamOrgScore
     */
    @Override
    @Transactional(readOnly = false)
    public void save(DcSupExamOrgScore dcSupExamOrgScore) {
        super.save(dcSupExamOrgScore);
        // 保存上传图片
        FileUploadUtils.saveFileUpload(dcSupExamOrgScore, dcSupExamOrgScore.getId(), "dcSupExamOrgScore_image");
        // 保存上传附件
        FileUploadUtils.saveFileUpload(dcSupExamOrgScore, dcSupExamOrgScore.getId(), "dcSupExamOrgScore_file");
    }

    /**
     * 更新状态
     *
     * @param dcSupExamOrgScore
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(DcSupExamOrgScore dcSupExamOrgScore) {
        super.updateStatus(dcSupExamOrgScore);
    }

    /**
     * 删除数据
     *
     * @param dcSupExamOrgScore
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(DcSupExamOrgScore dcSupExamOrgScore) {
        super.delete(dcSupExamOrgScore);
    }


    @Async
    @Transactional
    public void SumDcSupExamOrgScore(@NotBlank String examId, @NotBlank String exameOrg)  {
        log.info("开始计算总分examId：{}，exameOrg：{}", examId, exameOrg);
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        DcSupExamSumScore dcSupExamSumScore = new DcSupExamSumScore();
        dcSupExamSumScore.setExamId(examId);
        dcSupExamSumScore.setExameOrg(exameOrg);
        dcSupExamSumScore.setScoreSource("1");
        dcSupExamSumScore.setSumStatus("2");
        List<DcSupExamQuotaAdd> dcSupExamQuoatAdds = dcSupExamSumScoreDao.findDcSupExamQuoatAdd(examId, exameOrg);
        List<DcSupExamSumScore> dcSupExamSumScores = dcSupExamSumScoreDao.findList(dcSupExamSumScore);
        BigDecimal allScore = new BigDecimal("0.0");
        BigDecimal allMyScore = new BigDecimal("0.0");
        //计算加减指标分数
        for (DcSupExamQuotaAdd dcSupExamQuoatAdd : dcSupExamQuoatAdds) {
            String quotaClass = dcSupExamQuoatAdd.getQuotaClass();
            Double quotaScore = dcSupExamQuoatAdd.getQuotaScore();
            Double quotaWeight = dcSupExamQuoatAdd.getQuotaWeight();
            allScore=allScore.add(new BigDecimal(Double.toString(quotaWeight==null?0:quotaWeight)));
            if ("2".equals(quotaClass)) {
                allMyScore= allMyScore.add(new BigDecimal(Double.toString(quotaScore==null?0.0:quotaScore)));
            }else if ("3".equals(quotaClass)) {
                allMyScore= allMyScore.subtract(new BigDecimal(Double.toString(quotaScore==null?0.0:quotaScore)));
            }else {
                log.info("异常数据:{}",dcSupExamQuoatAdd);
            }

        }
        //计算汇总指标分数
        for (DcSupExamSumScore supExamSumScore : dcSupExamSumScores) {
            Double quotaWeight = supExamSumScore.getQuotaWeight();
            Double quotaScore = supExamSumScore.getQuotaScore();
            allScore=allScore.add(new BigDecimal(Double.toString(quotaWeight==null?0:quotaWeight)));
            allMyScore= allMyScore.add(new BigDecimal(Double.toString(quotaScore==null?0.0:quotaScore)));
        }

        Office office = EmpUtils.getOffice(exameOrg);
        DcSupExamOrgScore dcSupExamOrgScore = new DcSupExamOrgScore();
        dcSupExamOrgScore.setExamId(examId);
        dcSupExamOrgScore.setExameOrg(office.getOfficeCode());
        dcSupExamOrgScoreDao.deleteByEntity(dcSupExamOrgScore);
        dcSupExamOrgScore.setId(IdGen.nextId());
        dcSupExamOrgScore.setExameName(office.getOfficeName());
        dcSupExamOrgScore.setMyScore(allMyScore.doubleValue());
        dcSupExamOrgScore.setTotalScore(allScore.doubleValue());
        dcSupExamOrgScore.setCreateDate(new Date());
        log.info("开始计算总分结束dcSupExamOrgScore:{}",dcSupExamOrgScore);
        super.insert(dcSupExamOrgScore);
    }

    public List<DcSupExamOrgScore> findUnitListData(DcSupExamOrgScore dcSupExamOrgScore) {
        return dcSupExamSumScoreDao.findUnitListData(dcSupExamOrgScore);
    }

    public Page<DcSupExamSumQuotaInfo> indexListData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
        List<DcSupExamSumQuotaInfo> dcSupExamSumQuotaInfos = dcSupExamSumScoreDao.indexListData(dcSupExamSumQuotaInfo);
        return dcSupExamSumQuotaInfo.getPage().setList(dcSupExamSumQuotaInfos);
    }

    public Page<DcSupExamSumQuotaInfo> findDXRanKingListPage(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
        return dcSupExamSumQuotaInfo.getPage().setList(dcSupExamOrgScoreDao.findDXRanKingListPage(dcSupExamSumQuotaInfo));
    }

    public List<DcSupExamSumQuotaInfo> findUnitRankingScoreData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
        return dcSupExamOrgScoreDao.findUnitRankingScoreData(dcSupExamSumQuotaInfo);
    }
}